﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmDataDown :FrmBase
    {
        private Synchronize synchronize;
        private bool isSynchronizing = false;

        public FrmDataDown()
        {
            InitializeComponent();
            ttlbrhead.Text = "巡视管理";
            ilstbcup.Images.Add(ResourceImage.空白);
            ilstbcup.Images.Add(ResourceImage.空白);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.空白);
            ilstdown.Images.Add(ResourceImage.空白);
            ilstdown.Images.Add(ResourceImage.返回02);
        }

        private void FrmDataDown_Load(object sender, EventArgs e)
        {
            synchronize = new Synchronize();
            synchronize.OnSynchronizeProcess += new SynchronizeProcessHandler(synchronize_OnSynchronizeProcess);
            synchronize.OnGISDataSynchronizeProcess += new SynchronizeProcessHandler(synchronize_OnGISDataSynchronizeProcess);
        }

        /// <summary>
        /// 同步GIS数据过程
        /// </summary>
        /// <param name="info"></param>
        /// <param name="percent"></param>
        void synchronize_OnGISDataSynchronizeProcess(string info, int percent)
        {
            pbUpload.Visible = true;
            pbUpload.Image = ResourceImage.数据同步;
            pbSynchronize.Visible = true;
            isSynchronizing = true;

            lblInfo.Text = info;
            pbSynchronize.Value = percent;

            if (percent >= 100)
            {
                pbUpload.Visible = false;
                pbSynchronize.Visible = false;
                isSynchronizing = false;
            }

            this.Refresh();
        }

        void synchronize_OnSynchronizeProcess(string info, int percent)
        {
            pbUpload.Visible = true;
            pbUpload.Image = ResourceImage.数据同步;
            pbSynchronize.Visible = true;
            isSynchronizing = true;

            lblInfo.Text = info ;
            pbSynchronize.Value = percent;

            if (percent >= 100)
            {
                pbUpload.Visible = false ;
                pbSynchronize.Visible = false;
                isSynchronizing = false;
            }

            this.Refresh();
        }

        private void bcxunjian_OnButtonClick(object sender, int buttonIndex)
        {
            if (isSynchronizing)
                return;

            switch (buttonIndex)
            { case 2:
                    FormManage.GotoNext("FrmMain", null, null, false);
                    break;
            }
        }

        private void btnSynchronizeGIS_Click(object sender, EventArgs e)
        {
            if (isSynchronizing)
                return; 
            
            string info = synchronize.StartSynchrozieGISData();

            if (info != "") //同步出现问题
                lblInfo.Text = info;
            else
            {
                isSynchronizing = true;
            }
        }

        private void btnSynchronizeData_Click(object sender, EventArgs e)
        {
            if (DateTime.Now >= DateTime.Parse("2012-01-01"))
                return;

            if (isSynchronizing)
                return;

            string info = synchronize.StartSynchronize();

            if (info != "") //同步出现问题
                lblInfo.Text = info;
            else
            {
                isSynchronizing = true;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(synchronize.GetUrl());
        }


    }
}